#include <iostream>
#include <cmath>
using namespace std;
int pn(int x)
{
    bool p = true;
    int n;
    for (n = 2;n <= sqrt(x);n++)
           {

               if (x % n == 0)
               {
                    p = false;
                   
               }
           }
    if ((p == true) and (x!= 1))
    {
        return x;
    }
    else
    {
        return 0;
    }
}
 
int main()
{
    int a, i , i1, i2,n,x,im,t,max,b;
    i = 0;
    im = 0;
    int arr[4]{};
    int arrv[15]{};
    cin >> a;
    b = a;

    while (a > 0)
    {
        arr[i] = a % 10;
        a = a / 10;

        i = i+1;
        
    }


    for (i1 = 3;i1 >= 0;i1=i1-1) 
    {
        for (i2 = i1-1;i2 >= 0;i2=i2-1) 
        {
            x = arr[i1]*10+arr[i2];
            arrv[im]=pn(x);
            im = im+1;
            
        }
    }
    
    arrv[6]=pn(arr[3]*100+arr[2]*10+arr[1]);
    arrv[7]=pn(arr[3]*100+arr[1]*10+arr[0]);
    arrv[8]=pn(arr[3]*100+arr[2]*10+arr[0]);
    arrv[9]=pn(arr[2]*100+arr[1]*10+arr[0]);
    arrv[10]=pn(arr[3]*1000+arr[2]*100+arr[1]*10+arr[0]);
    arrv[11]=pn(arr[0]);
    arrv[12]=pn(arr[1]);
    arrv[13]=pn(arr[2]);
    arrv[14]=pn(arr[3]);
    max = 0;
    for (t=0;t<15;t++)
    {
        
        if (arrv[t]>max)
        {
            max = arrv[t];
        }
    }
    if ((b<1000) or (b>9999))
    {
        max = 0;
    }
    cout<<max;
    
    
}
